package cn.kgc.crowd1048.fundingadmin.service.impl;


import cn.kgc.crowd1048.fundingadmin.mapper.RoleMapper;
import cn.kgc.crowd1048.fundingadmin.pojo.Admin;
import cn.kgc.crowd1048.fundingadmin.pojo.Role;
import cn.kgc.crowd1048.fundingadmin.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwr
 * @since 2024-04-19
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    RoleMapper roleMapper;


    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public PageInfo<Role> queryForKeywordWithPage(Integer pageNum, Integer pageSize, String keyword) {
        // 1.开启分页功能
        PageHelper.startPage(pageNum, pageSize);

        // 2.执行查询
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        if (keyword!=null&&keyword.length()>0){
            wrapper.like("id",keyword)
                    .or()
                    .like("name",keyword);
        }
        List<Role> list = roleMapper.selectList(wrapper);
        // 3.封装为PageInfo对象
        return new PageInfo<>(list);

    }

    @Override
    public List<Role> getRoleListByIdList(List<Integer> roleIdList) {
        if (roleIdList == null || roleIdList.isEmpty()) {

            return null;
        }
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("id",roleIdList);
       return roleMapper.selectList(queryWrapper);
    }

    @Override
    public void batchRemove(List<Integer> roleIdList) {
        roleMapper.deleteBatchIds(roleIdList);
    }

    @Override
    public void saveRole(String roleName) {
        Role role=new Role(null,roleName);
        roleMapper.insert(role);
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateById(role);
    }

    @Override
    public List<Role> getAssignedRoleList(Integer adminId) {
        return roleMapper.getAssignedRoleList(adminId);
    }

    @Override
    public List<Role> getUnAssignedRoleList(Integer adminId) {
        return roleMapper.getUnAssignedRoleList(adminId);
    }

    @Override
    public void updateRelationship(Integer adminId, List<Integer> roleIdList) {
        //删除旧的
        roleMapper.deletAdminRelationRole(adminId);
        //添加新的
        roleMapper.insertAdminRelationRole(adminId,roleIdList);
    }
}
